package com.pactera.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.pactera.bean.UcResultBean;
import com.pactera.util.DateUtil;
import com.pactera.util.HttpClientUtil;
import com.pactera.util.JsonUtil;
import com.pactera.util.PropConfigUtil;
import com.pactera.util.SignUtil;
import com.pactera.vo.third.PrivVO;

@Service
public class UcUserService {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	private final String privKey = PropConfigUtil.getValueByKey("uc_priv_key");
	private final String baseUcUrl = PropConfigUtil.getValueByKey("uc_sys_url");
	private final String sysId = PropConfigUtil.getValueByKey("sys_id");
	
	public List<PrivVO> findPermissionByLoginName(String loginName) {
		UcResultBean resultBean = null;
		if(StringUtils.isBlank(loginName)){
			return new ArrayList<PrivVO>();
		}
		Integer timestamp = DateUtil.getSecondTimestamp(new Date());
		String ucUrl = "syspriv/list";
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("sysId", sysId);
		paramMap.put("userName", loginName);
		paramMap.put("timestamp", timestamp.toString());
		String paramStr = SignUtil.createLinkString(SignUtil.buildRequestPara4UC(paramMap, privKey));
		StringBuilder builder = new StringBuilder();
		builder.append(baseUcUrl).append(ucUrl).append("?").append(paramStr);
		logger.info(builder.toString());
		String jsonStr = HttpClientUtil.HttpGet(builder.toString());
		resultBean = JsonUtil.convertValue2Bean(jsonStr, UcResultBean.class);
		if(resultBean != null && 0 == resultBean.getCode()){
			String jsonString = JsonUtil.convertBean2Json(resultBean.getResult());
			return (List<PrivVO>) JsonUtil.convertValue2List(jsonString, ArrayList.class, PrivVO.class);
		}
		return new ArrayList<PrivVO>();
	}
}
